/**
 * @function Date2TimeStr
 * @description js Date Obj转Date Str
 * @param {Date} date
 * @param {string} fmt
 * @return {string} datetime string
 * @example
 */
export function Date2TimeStr(date, fmt = "yyyy-MM-dd hh:mm:ss") {
    var o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3),
        S: date.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return fmt;
}
/**
 * @function TimeStr2Date
 * @description '2021-09-09 00:00:00' 转js Date Obj
 * @param {string} dateTimeStr
 * @return {Date} Date Obj
 * @example
 */
export function TimeStr2Date(dateTimeStr) {
    return new Date(dateTimeStr); //"2021-09-09 00:00:00"
}
/**
 * @function TimeStr2TimeStamp
 * @description '2021-09-09 00:00:00' 转Unix时间戳 单位 秒
 * @param {string} dateTimeStr
 * @return {Date} Date Obj
 * @example
 */
export function TimeStr2TimeStamp(dateTimeStr) {
    let dateTime = TimeStr2Date(dateTimeStr);
    return dateTime.getTime() / 1000;
}
/**
 * @function TimeStamp2TimeStr
 * @description Unix时间戳 单位 秒 转 '2021-09-09 00:00:00'
 * @param {string} dateTimeStr
 * @return {Date} Date Obj
 * @example
 */
export function TimeStamp2TimeStr(timeStamp) {
    let dateTime = new Date(timeStamp * 1000);
    return Date2TimeStr(dateTime);
}
export function IsBrowserSupportLocalFonts() {
    if ("queryLocalFonts" in window) {
        return true;
    }
    else {
        return false;
    }
}
export async function GetLocalFontsPermissionState() {
    const { state } = await navigator.permissions.query({
        name: "local-fonts",
    });
    return state;
}
export function Pound2CM(pd) {
    return pd / 28.35; //1磅约等于0.03527厘米,1厘米约等于28.35磅
}
export function PartUpdate(srcObj, destObj, that) {
    Object.keys(srcObj).forEach((key) => {
        destObj[key] = srcObj[key];
        destObj.$set(destObj[key], key, srcObj[key]);
    });
}
export function UpdateConfigData(that, srcObj) {
    Object.keys(srcObj).forEach((key) => {
        that.$set(that.configData, key, srcObj[key]);
    });
}
export function CalcDocName(docName) {
    if (docName === undefined || docName == "") {
        return "空文档_" + GetRandStr();
    }
    return docName;
}
//export var ThinkEditorUtils =  {
function _PrefixInteger(numStr, n) {
    return (Array(n).join("0") + numStr.toString(16)).slice(-n);
}
/*******************
 * Color "ff010203"
 * ColorNum 0xff010203
 * RAGB rgba rgba.r
 * RAGBStr 'rgb(74, 238, 164)'或'rgba(74, 238, 164,1)'
 * ********************/
/**
 * @function Color2RGBA
 * @description 将"ff010203"转换为rgba对象
 * @param {string} color
 * @return {object} rgba rgba.r
 * @example
 */
export function Color2RGBA(color) {
    if (color.length % 2)
        color = "0" + color;
    let arr = [0x00, 0x00, 0x00, 0x00];
    let fillIndex = 0;
    for (let i = 0; i < color.length; i += 2) {
        let endPos = color.length - i;
        let startPos = endPos - 2;
        let sliceStr = color.substring(startPos, endPos);
        let a = parseInt(sliceStr, 16);
        arr[fillIndex++] = a;
    }
    let rgba = { a: arr[3] / 255.0, r: arr[2], g: arr[1], b: arr[0] };
    return rgba;
}
/**
 * @function RGBA2Color
 * @description 将rgba对象转换为字符串 "ff010203"
 * @param {object} rgba rgba.r
 * @return {string} "ff010203"
 * @example
 */
export function RGBA2Color(rgba) {
    var color = "";
    color += _PrefixInteger((rgba.a / 1.0) * 255, 2);
    color += _PrefixInteger(rgba.r, 2);
    color += _PrefixInteger(rgba.g, 2);
    color += _PrefixInteger(rgba.b, 2);
    return color;
}
/**
 * @function RGBA2ColorNum
 * @description 将rgba对象转换为数值 0xff010203
 * @param {object} rgba - rgba.r
 * @return {number} 0xff010203
 * @example
 */
export function RGBA2ColorNum(rgba) {
    return (((rgba.a / 1.0) * 255) << 24) | (parseInt(rgba.r) << 16) | ((rgba.g << 8) | rgba.b);
}
/**
 * @function ColorNum2RAGBStr
 * @description 将0xff010203转换为数值 'rgba(74, 238, 164,1)'
 * @param {object} rgba - rgba.r
 * @return {number} 0xff010203
 * @example
 */
export function ColorNum2RGBAStr(colorNum) {
    let a = (((colorNum >>> 24) & 0xff) / 255.0).toString();
    let r = ((colorNum >>> 16) & 0xff).toString();
    let g = ((colorNum >>> 8) & 0xff).toString();
    let b = (colorNum & 0xff).toString();
    return "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
}
/**
 * @function Color2RAGBStr
 * @description 将"ff010203"转换为数值 'rgba(74, 238, 164,1)'
 * @param {object} rgba - rgba.r
 * @return {number} 0xff010203
 * @example
 */
export function RGBA2RGBAStr(rgba) {
    let a = rgba.a.toString();
    let r = rgba.r.toString();
    let g = rgba.g.toString();
    let b = rgba.b.toString();
    return "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
}
export function RGBAStr2RGBA(rgbaStr) {
    var rgbaArr = rgbaStr
        .replace(/^(rgb|rgba)\(/, "")
        .replace(/\)$/, "")
        .replace(/\s/g, "")
        .split(",");
    var rgba = {};
    if (rgbaArr.length == 3) {
        rgba.r = parseInt(rgbaArr[0]);
        rgba.g = parseInt(rgbaArr[1]);
        rgba.b = parseInt(rgbaArr[2]);
    }
    else if (rgbaArr.length == 4) {
        rgba.r = parseInt(rgbaArr[0]);
        rgba.g = parseInt(rgbaArr[1]);
        rgba.b = parseInt(rgbaArr[2]);
        rgba.a = parseInt(rgbaArr[3]);
    }
    return rgba;
}
export function RGBAStr2Color(rgbaStr) {
    let rgba = RGBAStr2RGBA(rgbaStr);
    return RGBA2Color(rgba);
}
/**
 * @function Color2RAGBStr
 * @description 将"ff010203"转换为 'rgba(74, 238, 164,1)'
 * @param {object} rgba - rgba.r
 * @return {number} 0xff010203
 * @example
 */
export function Color2RGBAStr(color) {
    let rgba = Color2RGBA(color);
    return RGBA2RGBAStr(rgba);
}
export function ToPlainTextByCDATA(text) {
    return "<![CDATA[" + text + "]]>";
}
export function ToPlainTextByEscape(text) {
    function convert(substring) {
        switch (substring) {
            case "<":
                return "&lt;";
            case ">":
                return "&gt;";
            case "&":
                return "&amp;";
            case '"':
                return "&quot;";
            case "'":
                return "&apos;";
        }
        return substring;
    }
    return text.replace(/[<>"&']/g, convert);
}
export function ShowSaveFileDialog(fileName, dataBuffer) {
    let downLink = document.createElement("a");
    downLink.download = fileName;
    //字符内容转换为blod地址
    let blob = new Blob([dataBuffer]);
    downLink.href = URL.createObjectURL(blob);
    // 链接插入到页面
    document.body.appendChild(downLink);
    downLink.click();
    // 移除下载链接
    document.body.removeChild(downLink);
}
export function GetPrintIframe() {
    var iframe = document.getElementById("printIframe"); // .contentWindow.document.body;
    if (iframe === null) {
        iframe = document.createElement("IFRAME");
        iframe.id = "printIframe";
        iframe.setAttribute("style", "position:absolute;width:0px;height:0px;left:-500px;top:-500px;");
        document.body.appendChild(iframe);
        iframe.print_pages_cache = "";
    }
    return iframe;
}
export function GetRandStr() {
    let min = 100000000;
    let max = 999999999;
    let num = Math.floor(Math.random() * (max - min + 1)) + min;
    return num + "";
}
export function PopFileSelector(fileLimit) {
    return new Promise((resolve, reject) => {
        let input = document.createElement("input");
        input.value = "选择文件";
        input.type = "file";
        input.accept = fileLimit; //'.jpg,.svg,.bmp,.png,';
        input.onchange = function (e) {
            let file = e.target.files[0];
            let file_reader = new FileReader();
            file_reader.onload = (e) => {
                const arrayBuffer = e.target.result;
                const fileName = file.name.split(".").slice(0, -1).join("."); //去掉.xml
                resolve({ file: file.name, fileName: fileName, data: arrayBuffer }); // 返回文件文本内容到
            };
            file_reader.readAsArrayBuffer(file);
        };
        input.click();
    });
}
// dataURLToCanvas(dataurl, cb) {
//   var canvas = this.instance.canvas;
//   var ctx = canvas.getContext('2d');
//   var img = new Image();
//   img.onload = function () {
//     canvas.width = img.width;
//     canvas.height = img.height;
//     ctx.drawImage(img, 0, 0);
//     //cb(canvas);
//   };
//   img.src = dataurl;
// }
// fileOrBlobToDataURL(obj, cb) {
//   var a = new FileReader();
//   a.readAsDataURL(obj);
//   a.onload = function (e) {
//     cb(e.target.result);
//   };
// }
// BlobToCanvas(blob, cb) {
//   ThinkEditor.fileOrBlobToDataURL(blob, function (dataurl) {
//     ThinkEditor.dataURLToCanvas(dataurl, cb);
//   });
// }
